<?php

class Librarian_TransactionController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $q = trim($this->getRequest()->getParam('q', ''));

        $date = trim($this->getRequest()->getParam('date', ''));

        $page = intval($this->getRequest()->getParam('page', 1));

        $type = intval($this->getRequest()->getParam('type', 0));

        $params = array(
            'Page'   => $page,
            'Limit'  => 10,
            'q'      => $q,
            'Status' => 1,
            'Type'   => $type
        );

        $params['FromDate'] = $params['ToDate']   = 0;

        if ($date)
        {
            list($d, $m, $y) = explode('/', $date);

            if ($d && $m && $y && checkdate($m, $d, $y))
            {
                $params['FromDate'] = mktime(0, 0, 1, $m, $d, $y);
                $params['ToDate']   = mktime(23, 59, 59, $m, $d, $y);
            }
        }

        $accounts = Model_Account::getAll(array(
                    'Type'   => 1,
                    'Limit'  => 1000000,
                    'Page'   => 1,
                    'Status' => 1
                        ), $c);

        $transactions = Model_Transaction::find($params, $total);
        $paginator    = Core_Paginator::get($total, $page);

        $arrAccounts = array();

        foreach ($accounts as $account)
        {
            $arrAccounts[] = $account['Name'];
        }

        $this->view->assign('pagination', $paginator);
        $this->view->assign('accounts', '["' . implode('","', $arrAccounts) . '"]');
        $this->view->assign('transactions', $transactions);
    }

    public function editAction()
    {
        if ($this->getRequest()->isPost())
        {
            $params = $this->getRequest()->getPost();

            $transactionId = Model_Transaction::edit($params);

            if ($transactionId > 0)
            {
                return $this->_helper->json(array(
                            'status'         => 1,
                            'message'        => 'Success',
                            'transaction_id' => $transactionId
                        ));
            }

            return $this->_helper->json(array(
                        'status'  => 0,
                        'message' => 'Unknown error...'
                    ));
        }
    }

    public function deleteAction()
    {
        if ($this->getRequest()->isPost())
        {
            $params = $this->getRequest()->getPost();

            Model_Transaction::delete($params);

            return $this->_helper->json(array(
                        'status'  => 1,
                        'message' => 'Success',
                    ));
        }
    }

}